<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="java.util.ArrayList"%>
    <%@ page import="java.sql.*"%>

<%@page import="Datenbankhandler.DBConn"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Artikel suchen</title>
<style type="text/css">
td {}
.start{vertical-align:top}
.name{vertical-align:top}
</style>
</head>
<body>
<table border="0" width="100%">
  <colgroup>
    <col width="62*">
    <col width="2*">
    <col width="1*">  
  </colgroup>
  <tr>
    <td class="name" align="left"><font color="#DD7711">Hallo</font>,
    <%@ page language="java" %>
    <%
    if(session.getAttribute("login") == null){
    	
    	session.setAttribute("login","Gast");
    }		
    %>
 <%= session.getAttribute("login") %>
    </td>
    
    
    
    <td class="start" align="right"><a href="index.jsp">Startseite</a></td>
  </tr>
</table>
<br>
<div>

<center><h2><font color="#DD1100">i</font><font color="#229900">Verkauf</font></h2></center>
<center><h1>Suche</h1>
<form action="Suchergebnis.jsp" name="asdasd">
  <center>
  <p><input name="Suchfeld" type="text" size="60" maxlength="60"></p>
  </center>

<table border="0" align="center">
 <tr>
   <td>
   <form name="Formular" action="UserServlet" onsubmit="return chkFormular()" method="post">
</div>
<br>
<div>
<center>Sortieren nach: 
<!--<select name="kategorie" onchange="document.forms['asdasd'].submit();"> -->
<select name="kategorie">
 <option  value="name">Name</option>
 <option  value="preis">Bieterpreis</option>
 <option   value="gebote">Kategorie</option>
 <option  value="dauer">Dauer</option>	
</center>
</select>

</form>
</div>

<br>
<br>
<center>
<%@ page import="java.sql.*" isThreadSafe="false" %>
<%


  Connection conn=DBConn.getConnection();
 	String eingabe= request.getParameter("Suchfeld");
 	String sortname = request.getParameter("kategorie");
 	System.out.println(sortname);
 	String sSql   = "Select ID,Bezeichnung,Kategorie,Bieterpreis,Sofortpreis, Enddauer FROM Artikel where Lebensdauer='1'"  ;    
 	String sSql2= sSql+ " and lower(Bezeichnung) LIKE lower('" + eingabe + "%')";
 	
    Statement  st = null;
    ResultSet  rs = null;
  
    try {
      st = conn.createStatement();
      
      	st.executeUpdate("ALTER session SET NLS_SORT = 'BINARY_CI'");

 		if(eingabe=="" && sortname.equals("name")){
    	  System.out.println("Bin in eingabenull name");
    	  String sSql3=sSql2 + " order by Bezeichnung";
    	  rs=st.executeQuery(sSql3);
      }
      
      
      if(eingabe=="" && sortname.equals("preis")){
    	  System.out.println("Bin in eingabenull preis");
    	  String sSql3=sSql2 + " order by Bieterpreis";
      rs = st.executeQuery(sSql3);
      }
      
      if(eingabe=="" && sortname.equals("gebote")){
    	  System.out.println("Bin in eingabenull gebote");
    	  String sSql3=sSql2 + " order by Kategorie";
      rs = st.executeQuery(sSql3);
      }
      if(eingabe=="" && sortname.equals("dauer")){
    	  System.out.println("Bin in eingabenull dauer");
    	  String sSql3=sSql2 + " order by Enddauer";
      rs = st.executeQuery(sSql3);
      }      
      if(!(eingabe=="") && sortname.equals("name")){
    	  System.out.println("Bin in eingabe ungleichnull name");
    	  String sSql3=sSql2 + " order by Bezeichnung";
    	  rs=st.executeQuery(sSql3);
      }
    
      if(!(eingabe=="") && sortname.equals("preis")){
    	  System.out.println("Bin in eingabe ungleichnull Bieterpreis");
    	  String sSql3=sSql2 + " order by Preis";
    	  rs=st.executeQuery(sSql3);
      }
      if(!(eingabe=="") && sortname.equals("gebote")){
    	  System.out.println("Bin in eingabe ungleichnull kate");
    	  String sSql3=sSql2 + " order by Kategorie";
    	  rs=st.executeQuery(sSql3);
      }
      if(!(eingabe=="") && sortname.equals("dauer")){
    	  System.out.println("Bin in eingabe ungleichnull dauer");
    	  String sSql3=sSql2 + " order by Enddauer";
    	  rs=st.executeQuery(sSql3);
      }
     

      
   
      
      ResultSetMetaData rsmd = rs.getMetaData();
      int n = rsmd.getColumnCount();
      out.println( "<table border=30 cellspacing=5><tr>" );
      for( int i=1; i<=n; i++ )    
        out.println( "<th>" + rsmd.getColumnName( i ) + "</th>" );
      while( rs.next() )
      {
        out.println( "</tr><tr>" );
        for(int i=1;i<=1; i++){
        	
        	out.println("<td><a href='InformationServlet?id="+rs.getString(i)+"'>" + rs.getString(i)+ "</a></td>");
        	System.out.println(request.getParameter("artikelid"));
        }
        for( int i=2; i<=n; i++ )  {
          out.println( "<td>" + rs.getString( i ) + "</td>" ); 
          

        }
      }
      
      out.println( "</tr></table>" );
      
    } finally {
    	DBConn.closeConnection();
    }
 
    
    
    
    
    
    
    
%>
</center>
</body>
</html>